
package org.columba.addressbook.gui.autocomplete;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.deruan.hotcore.contact.model.BasicModelPartial;
import com.deruan.hotcore.contact.model.IBasicModelPartial;


public class AddressCollector implements IAddressCollector {

	private Hashtable _adds = new Hashtable();

	private static AddressCollector instance = new AddressCollector();

	private AddressCollector() {
	}

	public static AddressCollector getInstance() {
		return instance;
	}

	/**
	 * Add all contacts and group items to hashmap.
	 * 
	 * @param uid
	 *            selected folder uid
	 * @param includeGroup
	 *            add groups if true. No groups, otherwise.
	 */
	public void addAllContacts(String uid, boolean includeGroup) {
		if (uid == null)
			throw new IllegalArgumentException("uid == null");

		List<IBasicModelPartial> list = new Vector<IBasicModelPartial>();

		try{

			//list.addAll(folder.getHeaderItemList());

		} catch (Exception e) {

			e.printStackTrace();
		}

		Iterator it = list.iterator();
		while (it.hasNext()) {
			BasicModelPartial headerItem = (BasicModelPartial) it.next();

			if (headerItem.isContact()) {
				// contacts item
				//ContactModelPartial item = (ContactModelPartial) headerItem;

				//addAddress(item.getName(), item);
				//addAddress(item.getLastname(), item);
				//addAddress(item.getFirstname(), item);
				//addAddress(item.getAddress(), item);
			}
		}
	}

	public void addAddress(String add, IBasicModelPartial item) {
		if (add != null) {
			_adds.put(add, item);
		}
	}

	public Object[] getAddresses() {
		return _adds.keySet().toArray();
	}

	public IBasicModelPartial getHeaderItem(String add) {
		return (IBasicModelPartial) _adds.get(add);
	}

	public void clear() {
		_adds.clear();
	}

	/**
	 * @see org.frappucino.addresscombobox.ItemProvider#getMatchingItems(java.lang.String)
	 */
	public Object[] getMatchingItems(String s) {
		Object[] items = getAddresses();

		Vector v = new Vector();
		// for each JComboBox item
		for (int k = 0; k < items.length; k++) {
			// to lower case
			String item = items[k].toString().toLowerCase();
			// compare if item starts with str
			if (item.startsWith(s.toLowerCase())) {
				v.add(item);
			}
		}
		return v.toArray();
	}
}